package util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;

import testutil.ScoreComputationBeanRemote;

import dao.TestQuestionDAORemote;
import dto.CandidateDTO;
import dto.CategoryDTO;
import dto.DomainDTO;

import dto.EmployeeDTO;
import dto.InterviewDTO;
import dto.QuestionDTO;
import dto.QuestionValidDTO;
import dto.RoomDTO;
import dto.TemplateCategoryDTO;
import dto.TemplateDTO;
import dto.TestQuestionDTO;
import entities.Candidate;
import entities.Category;
import entities.Domain;

import entities.Employee;
import entities.Interview;
import entities.Question;
import entities.Room;
import entities.Template;
import entities.TemplateCategory;
import entities.TestQuestion;

public class Conversion {
	
	@EJB TestQuestionDAORemote testQuestionDAO;
	@EJB ScoreComputationBeanRemote scoreComputationDAO;

	public Category fromCategoryDTOToCategory(CategoryDTO cDTO) throws Exception {
		Conversion conversie = new Conversion();
		try {
			Category c = new Category();
			c.setName(cDTO.getName());
			c.setDomain(conversie.fromDomainDTOtoDomain(cDTO.getDomain()));

			return c;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from CategoryDTO to Category", e);
		}
	}

	public CategoryDTO fromCategoryToCategoryDTO(Category c) throws Exception {
		try {
			CategoryDTO cDTO = new CategoryDTO();
			cDTO.setId(c.getId());
			cDTO.setName(c.getName());
			return cDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Category to CategoryDTO", e);
		}
	}

	public Room fromRoomDTOToRoom(RoomDTO rDTO) throws Exception {

		try {
			Room r = new Room();
			r.setId(rDTO.getId());
			r.setAddress(rDTO.getAddress());
			r.setNumberOfStations(rDTO.getNumberOfStations());

			return r;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from RoomDTO to Room", e);
		}
	}

	public RoomDTO fromRoomToRoomDTO(Room r) throws Exception {
		try {
			RoomDTO rDTO = new RoomDTO();
			rDTO.setId(r.getId());
			rDTO.setAddress(r.getAddress());
			rDTO.setNumberOfStations(r.getNumberOfStations());

			return rDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Room to RoomDTO", e);
		}
	}

	public QuestionValidDTO fromQuestionToQuestionValidDTO(Question q) throws Exception {

		List<String> listAll = new ArrayList<String>();
		List<String> listCorrect = new ArrayList<String>();

		try {
			QuestionValidDTO qDTO = new QuestionValidDTO();
			qDTO.setId(q.getId());
			qDTO.setText(q.getText());
			qDTO.setAnswer1(q.getAnswer1());
			qDTO.setAnswer2(q.getAnswer2());
			qDTO.setAnswer3(q.getAnswer3());
			qDTO.setAnswer4(q.getAnswer4());

			listAll.add(q.getAnswer1());
			listAll.add(q.getAnswer2());
			listAll.add(q.getAnswer3());
			listAll.add(q.getAnswer4());
			qDTO.setAllAnswerList(listAll);

			if (q.getCorrectAnswers() > 9) {
				int i = q.getCorrectAnswers() / 10;
				int j = q.getCorrectAnswers() % 10;

				listCorrect.add(listAll.get(i-1));
				listAll.remove(i-1);
				listCorrect.add(listAll.get(j-2));
				listAll.remove(j-2);

			}
			
			if (q.getCorrectAnswers() < 10) {
				int k = q.getCorrectAnswers();
				listCorrect.add(listAll.get(k-1));
				listAll.remove(k-1);
			}

			qDTO.setCorrectAnswerList(listCorrect);
			
			qDTO.setCorrectAnswer(q.getCorrectAnswers());
			qDTO.setValid(q.getValid());
			return qDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Question to QuestionValidDTO", e);
		}
	}

	public Question fromQuestionValidDTOToQuestion(QuestionValidDTO qDTO) throws Exception {
		try {
			Question q = new Question();
			q.setText(qDTO.getText());
			q.setAnswer1(qDTO.getAnswer1());
			q.setAnswer2(qDTO.getAnswer2());
			q.setAnswer3(qDTO.getAnswer3());
			q.setAnswer4(qDTO.getAnswer4());
			q.setCorrectAnswers(qDTO.getCorrectAnswer());
			q.setValid(qDTO.getValid());
			return q;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from QuestionValidDTO to Question", e);
		}
	}

	public Question fromQuestionDTOToQuestion(QuestionDTO qDTO) throws Exception {
		try {
			Question q = new Question();
			q.setAnswer1(qDTO.getAnswer1());
			q.setAnswer2(qDTO.getAnswer2());
			q.setAnswer3(qDTO.getAnswer3());
			q.setAnswer4(qDTO.getAnswer4());

			q.setCategory(fromCategoryDTOToCategory(qDTO.getCategory()));

			q.setCorrectAnswers(qDTO.getCorrectAnswers());
			List<TestQuestionDTO> ltq = qDTO.getTestQuestions();
			List<TestQuestion> lq = new ArrayList<TestQuestion>();
			if (ltq != null)
				for (TestQuestionDTO t : ltq) {
					lq.add(fromTestQuestionDTOToTestQuestion(t));
				}

			q.setTestQuestions(lq);
			q.setText(qDTO.getText());
			q.setValid(qDTO.getValid());

			return q;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Couldn't convert from QuestionDTO to Question", e);
		}

	}

	// do not modify this method!
	public Question fromQuestionDTOToQuestionWithoutCateg(QuestionDTO qDTO) throws Exception {
		try {
			Question q = new Question();
			q.setAnswer1(qDTO.getAnswer1());
			q.setAnswer2(qDTO.getAnswer2());
			q.setAnswer3(qDTO.getAnswer3());
			q.setAnswer4(qDTO.getAnswer4());
			q.setCorrectAnswers(qDTO.getCorrectAnswers());
			q.setText(qDTO.getText());
			q.setValid(qDTO.getValid());
			return q;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Couldn't convert from QuestionDTO to Question", e);
		}
	}

	public QuestionDTO fromQuestionToQuestionDTO(Question q) throws Exception {
		try {
			QuestionDTO qDTO = new QuestionDTO();
			qDTO.setId(q.getId());
			qDTO.setAnswer1(q.getAnswer1());
			qDTO.setAnswer2(q.getAnswer2());
			qDTO.setAnswer3(q.getAnswer3());
			qDTO.setAnswer4(q.getAnswer4());
			qDTO.setCategory(fromCategoryToCategoryDTO(q.getCategory()));
			qDTO.setCorrectAnswers(q.getCorrectAnswers());
			qDTO.setText(q.getText());
			qDTO.setValid(q.getValid());
			return qDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Question to QuestionDTO", e);
		}
	}

	public TestQuestion fromTestQuestionDTOToTestQuestion(TestQuestionDTO tqDTO) throws Exception {
		try {
			TestQuestion tq = new TestQuestion();
			tq.setCandidateAnswer(tqDTO.getCandidateAnswer());
			tq.setId(tqDTO.getId());
			// tq.setInterview( fromInterviewDTOToInterview(
			// tqDTO.getInterview()));
			tq.setQuestion(fromQuestionDTOToQuestion(tqDTO.getQuestion()));
			return tq;

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Couldn't convert from TestQuestionDTO to TestQuestion", e);
		}
	}

	public TestQuestionDTO fromTestQuestionToTestQuestionDTO(TestQuestion tq) throws Exception {
		try {
			TestQuestionDTO tqDTO = new TestQuestionDTO();
			tqDTO.setId(tq.getId());
			tqDTO.setCandidateAnswer(tq.getCandidateAnswer());
			tqDTO.setQuestion(fromQuestionToQuestionDTO(tq.getQuestion()));
			// tqDTO.setInterview(fromInterviewToInterviewDTO(tq.getInterview()));
			return tqDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from TestQuestion to TestQuestionDTO", e);
		}
	}

	public DomainDTO fromDomainToDomainDTO(Domain d) throws Exception {
		try {
			DomainDTO dDTO = new DomainDTO();
			dDTO.setId(d.getId());
			dDTO.setName(d.getName());

			return dDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Domain to DomainDTO", e);
		}

	}

	public Domain fromDomainDTOtoDomain(DomainDTO dDTO) throws Exception {
		try {
			Domain d = new Domain();

			// List<Category> lc=new ArrayList<Category>();
			// for(CategoryDTO c:dDTO.getCategories())
			// lc.add(fromCategoryDTOToCategory(c));
			//			
			// d.setCategories(lc);
			d.setName(dDTO.getName());

			return d;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from DomainDTO to Domain", e);
		}

	}

	public Interview fromInterviewDTOToInterviewWithoutForeign(InterviewDTO iDTO) throws Exception {
		try {
			Interview i = new Interview();
			i.setAccepted(iDTO.getAccepted());
			//i.setCandidate(fromCandidateDTOToCandidate(iDTO.getCandidate()));
			i.setComment(iDTO.getComment());
			i.setDate(iDTO.getDate());
			//i.setEmployee(fromEmployeeDTOToEmployee(iDTO.getEmployee()));
			i.setEndTime(iDTO.getEndTime());
			//i.setId(iDTO.getId());
			//i.setRoom(fromRoomDTOToRoom(iDTO.getRoom()));
			i.setStartTime(iDTO.getStartTime());
			//i.setTemplate(fromTemplateDTOToTemplate(iDTO.getTemplate()));

//			List<TestQuestionDTO> ltq = iDTO.getTestQuestions();
//			List<TestQuestion> lt = new ArrayList<TestQuestion>();
//			for (TestQuestionDTO t : ltq) {
//				lt.add(fromTestQuestionDTOToTestQuestion(t));
//			}
//			i.setTestQuestions(lt);

			return i;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Couldn't convert from InterviewDTO to Interview", e);
		}
	}
	
	
	
	public Interview fromInterviewDTOToInterview(InterviewDTO iDTO) throws Exception {
		try {
			Interview i = new Interview();
			i.setAccepted(iDTO.getAccepted());
			i.setCandidate(fromCandidateDTOToCandidate(iDTO.getCandidate()));
			i.setComment(iDTO.getComment());
			i.setDate(iDTO.getDate());
			i.setEmployee(fromEmployeeDTOToEmployee(iDTO.getEmployee()));
			i.setEndTime(iDTO.getEndTime());
			i.setId(iDTO.getId());
			i.setRoom(fromRoomDTOToRoom(iDTO.getRoom()));
			i.setStartTime(iDTO.getStartTime());
			i.setTemplate(fromTemplateDTOToTemplate(iDTO.getTemplate()));

			List<TestQuestionDTO> ltq = iDTO.getTestQuestions();
			List<TestQuestion> lt = new ArrayList<TestQuestion>();
			for (TestQuestionDTO t : ltq) {
				lt.add(fromTestQuestionDTOToTestQuestion(t));
			}
			i.setTestQuestions(lt);

			return i;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Couldn't convert from InterviewDTO to Interview", e);
		}
	}

	public InterviewDTO fromInterviewToInterviewDTO(Interview i) throws Exception {
		try {
			InterviewDTO iDTO = new InterviewDTO();
			iDTO.setId(i.getId());
			iDTO.setAccepted(i.getAccepted());
			iDTO.setCandidate(fromCandidateToCandidateDTO(i.getCandidate()));
			iDTO.setComment(i.getComment());
			iDTO.setDate(i.getDate());
			iDTO.setEmployee(fromEmployeeToEmployeeDTO(i.getEmployee()));
			iDTO.setEndTime(i.getEndTime());
			iDTO.setRoom(fromRoomToRoomDTO(i.getRoom()));
			iDTO.setStartTime(i.getStartTime());
			
			iDTO.setTemplate(fromTemplateToTemplateDTO(i.getTemplate()));
			List<TestQuestion> tqList = i.getTestQuestions();
			List<TestQuestionDTO> tqDTOList = new ArrayList<TestQuestionDTO>();
			System.out.println("Int " + i.getId() + " - " + i.getTestQuestions().size() );
			for (TestQuestion tq : tqList) {
				tqDTOList.add(fromTestQuestionToTestQuestionDTO(tq));
				System.out.println("FROM inter->DTO " + tq.getCandidateAnswer());
			}
			iDTO.setTestQuestions(tqDTOList);
			//List<TestQuestionDTO> l=testQuestionDAO.getInterviewQuestions(i.getId());
			boolean okGenerate=true;
			boolean okStart=false;
			boolean okTaken=false;
			if(i.getTestQuestions().size()!=0){
				for(TestQuestion tq:i.getTestQuestions())
					if(tq.getCandidateAnswer()!=-1){
						okTaken=true;
						break;
					}
			}
			if(i.getTestQuestions().size()==0){
				okGenerate=false;
				okStart=true;
			}
			for(TestQuestion tq:i.getTestQuestions())
				if(tq.getCandidateAnswer()!=-1){
					okGenerate=false;
					okStart=true;
				}
			iDTO.setGenerated(okGenerate);
			if(i.getAccepted()==-1)
				iDTO.setAcceptedString("N/A");
			else
				if(i.getAccepted()==0)
					iDTO.setAcceptedString("No");
				else iDTO.setAcceptedString("Yes");
			if(okGenerate==false)
				iDTO.setGenerateButtonStyleClass("button");
			else
				iDTO.setGenerateButtonStyleClass("buttonDis");
			if(okStart==false)
				iDTO.setStartButtonStyleClass("button");
			else
				iDTO.setStartButtonStyleClass("buttonDis");
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			iDTO.setDateString(sdf.format(i.getDate()));
			iDTO.setStartTest(okStart);
			//System.out.println(scoreComputationDAO.computeTotalScore(i.getId()));
			if(okTaken==true)
				iDTO.setTaken("check.png");
			else
				iDTO.setTaken("cross.png");
			return iDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Interview to InterviewDTO", e);
		}
	}
	
	public InterviewDTO fromInterviewToInterviewDTOOnlyEmplAndCandid(Interview i) throws Exception {
		try {
			InterviewDTO iDTO = new InterviewDTO();
			iDTO.setId(i.getId());
			iDTO.setAccepted(i.getAccepted());
			iDTO.setCandidate(fromCandidateToCandidateDTO(i.getCandidate()));
			iDTO.setComment(i.getComment());
			iDTO.setDate(i.getDate());
			iDTO.setEmployee(fromEmployeeToEmployeeDTO(i.getEmployee()));
			iDTO.setEndTime(i.getEndTime());
			iDTO.setStartTime(i.getStartTime());
			
			
			return iDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Interview to InterviewDTO", e);
		}
	}
	
	

	public Template fromTemplateDTOToTemplate(TemplateDTO tDTO) throws Exception {
		try {
			Template t = new Template();
			t.setDuration(tDTO.getDuration());
			t.setId(tDTO.getId());
			t.setTitle(tDTO.getTitle());
			// DON'T PUT THIS BACK IN THE CODE...THERE'S A LOOP IN IT AND IT
			// OVERFLOWS THE FRIGGING STACK
			// List<Interview> li=new ArrayList<Interview>();
			// for(InterviewDTO i:tDTO.getInterviews())
			// li.add(fromInterviewDTOToInterview(i));
			// t.setInterviews(li);

			// List<TemplateCategory> ltc = new ArrayList<TemplateCategory>();
			// List<TemplateCategoryDTO> ltcDTO = tDTO.getTemplateCategories();
			// System.out.println("size " + ltcDTO.size());
			// for(TemplateCategoryDTO tt : ltcDTO) {
			// System.out.println("kill me now");
			// ltc.add(fromTemplateCategoryDTOtoTemplateCategory(tt));
			// }
			// t.setTemplateCategories(ltc);
			return t;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from TemplateDTO To Template", e);
		}
	}

	public TemplateDTO fromTemplateToTemplateDTO(Template t) throws Exception {
		try {
			TemplateDTO tDTO = new TemplateDTO();
			tDTO.setDuration(t.getDuration());
			tDTO.setId(t.getId());
			tDTO.setTitle(t.getTitle());

			return tDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Template To TemplateDTO", e);
		}
	}

	public TemplateCategory fromTemplateCategoryDTOtoTemplateCategory(TemplateCategoryDTO tcDTO)
			throws Exception {
		try {
			TemplateCategory tc = new TemplateCategory();
			tc.setCategory(fromCategoryDTOToCategory(tcDTO.getCategory()));
			tc.setId(tcDTO.getId());
			tc.setNumberOfQuestions(tcDTO.getNumberOfQuestions());
			tc.setTemplate(fromTemplateDTOToTemplate(tcDTO.getTemplate()));
			return tc;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from TemplateCategoryDTO to TemplateCategory", e);
		}
	}

	public TemplateCategory fromTemplateCategoryDTOtoTemplateCategory(TemplateCategoryDTO tcDTO,
			Template t, Category c) throws Exception {
		try {
			TemplateCategory tc = new TemplateCategory();
			tc.setCategory(c);
			tc.setId(tcDTO.getId());
			tc.setNumberOfQuestions(tcDTO.getNumberOfQuestions());
			tc.setTemplate(t);
			return tc;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from TemplateCategoryDTO to TemplateCategory", e);
		}
	}

	public TemplateCategoryDTO fromTemplateCategoryToTemplateCategoryDTO(TemplateCategory tc)
			throws Exception {
		try {
			TemplateCategoryDTO tcDTO = new TemplateCategoryDTO();
			tcDTO.setCategory(fromCategoryToCategoryDTO(tc.getCategory()));
			tcDTO.setId(tc.getId());
			tcDTO.setNumberOfQuestions(tc.getNumberOfQuestions());
			tcDTO.setTemplate(fromTemplateToTemplateDTO(tc.getTemplate()));
			return tcDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from TemplateCategory to TemplateCategoryDTO", e);
		}
	}

	public Employee fromEmployeeDTOToEmployee(EmployeeDTO eDTO) throws Exception {
		try {
			Employee e = new Employee();
			e.setEmail(eDTO.getEmail());
			e.setId(eDTO.getId());
			e.setName(eDTO.getName());
			e.setPassword(eDTO.getPassword());
			e.setRank(eDTO.getRank());
			e.setUsername(eDTO.getUsername());
			
//		    List<InterviewDTO> li = eDTO.getInterviews();
//			List<Interview> l = new ArrayList<Interview>();
//			for (InterviewDTO i : li)
//				l.add(fromInterviewDTOToInterview(i));
//
//			e.setInterviews(l);
			

			return e;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from EmployeeDTO to Employee", e);
		}
	}

	public Candidate fromCandidateDTOToCandidate(CandidateDTO cDTO) throws Exception {
		try {
			Candidate c = new Candidate();
			c.setEmail(cDTO.getEmail());
			c.setFirstName(cDTO.getFirstName());
			c.setLastName(cDTO.getLastName());
			c.setLink(cDTO.getLink());
			// if Candidate must be updated
			if (cDTO.getId() != -1) {
				c.setId(cDTO.getId());
			}
			c.setEmail(cDTO.getEmail());
			c.setFirstName(cDTO.getFirstName());
			c.setLastName(cDTO.getLastName());
			c.setLink(cDTO.getLink());
			return c;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from CandidateDTO to Candidate", e);
		}
	}

	public CandidateDTO fromCandidateToCandidateDTO(Candidate c) throws Exception {
		try {
			CandidateDTO cDTO = new CandidateDTO();
			cDTO.setId(c.getId());
			cDTO.setEmail(c.getEmail());
			cDTO.setFirstName(c.getFirstName());
			cDTO.setLastName(c.getLastName());
			cDTO.setLink(c.getLink());
			List<Integer> tempInt = new ArrayList<Integer>();
			for (Interview i : c.getInterviews()) {
				tempInt.add(i.getId());
			}
			cDTO.setInterviews(tempInt);
			return cDTO;
		} catch (Exception e) {
			throw new Exception("Couldn't convert from Candidate to CandidateDTO", e);
		}
	}

	public EmployeeDTO fromEmployeeToEmployeeDTO(Employee em) throws Exception {
		try {
			EmployeeDTO eDTO = new EmployeeDTO();
			eDTO.setId(em.getId());
			eDTO.setUsername(em.getUsername());
			eDTO.setPassword(em.getPassword());
			eDTO.setRank(em.getRank());
			eDTO.setName(em.getName());
			eDTO.setEmail(em.getEmail());
			
			return eDTO;
		}

		catch (Exception e) {
			throw new Exception("Couldn't convert from Employee to EmployeeDTO", e);
		}

	}
	
	public EmployeeDTO fromEmployeeToEmployeeDTOWithInterviews(Employee em,List<Interview> li) throws Exception {
		try {
			EmployeeDTO eDTO = new EmployeeDTO();
			eDTO.setId(em.getId());
			eDTO.setUsername(em.getUsername());
			eDTO.setPassword(em.getPassword());
			eDTO.setRank(em.getRank());
			eDTO.setName(em.getName());
			eDTO.setEmail(em.getEmail());
			
			List<InterviewDTO> liDTO=new ArrayList<InterviewDTO>();
			for(Interview i:li){
				liDTO.add(fromInterviewToInterviewDTOOnlyEmplAndCandid(i));
			}
			eDTO.setInterviews(liDTO);
			
			return eDTO;
		}

		catch (Exception e) {
			throw new Exception("Couldn't convert from Employee to EmployeeDTO", e);
		}

	}
}
